package cn.rokhdelar.ams.management.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.rokhdelar.ams.common.domain.vo.PageVO;
import cn.rokhdelar.ams.management.domain.po.SysRole;
import cn.rokhdelar.ams.management.domain.query.RoleQuery;
import cn.rokhdelar.ams.management.domain.vo.RoleVO;
import cn.rokhdelar.ams.management.service.SysRoleService;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Rokhdelar
 * @since 2024/10/15 -- 12:11
 **/

@RestController
@RequestMapping(value = "/management/roles")
@RequiredArgsConstructor
@Schema(title = "角色接口", description = "角色相关的接口")
public class RoleController {
    private final SysRoleService roleService;

    @GetMapping
    @Schema(title = "获取所有角色", description = "根据查询条件返回当前数据库中的所有角色")
    public ResponseEntity<PageVO<RoleVO>> listRoles(@ParameterObject RoleQuery query) {
        return ResponseEntity.ok(roleService.getRoleListByPage(query));
    }

    @GetMapping("/{id}")
    @Schema(title = "通过id获取角色", description = "根据指定的id返回当前数据库中对应的角色")
    public ResponseEntity<RoleVO> getRole(@PathVariable String id) {
        return ResponseEntity.ok(roleService.getRoleById(id));
    }

    @PostMapping
    @Schema(title = "创建角色", description = "创建一个新的角色")
    public ResponseEntity<Boolean> createRole(@RequestBody RoleVO role) {
        return ResponseEntity.ok(roleService.createRole(role));
    }

    @PatchMapping(value = "/{id}")
    @Schema(title = "更新角色", description = "根据id更新一个已存在的角色")
    public ResponseEntity<Boolean> updateRole(@PathVariable String id, @RequestBody RoleVO role) {
        return ResponseEntity.ok(roleService.updateRole(role));
    }

    @DeleteMapping(value = "/{id}")
    @Schema(title = "删除角色", description = "根据id删除一个已存在的角色")
    public ResponseEntity<Boolean> deleteRole(@PathVariable String id) {
        return ResponseEntity.ok(roleService.removeById(id));
    }
}
